/******************************************************************************
 * Product: Adempiere ERP & CRM Smart Business Solution                       *
 * Copyright (C) 1999-2007 ComPiere, Inc. All Rights Reserved.                *
 * This program is free software, you can redistribute it and/or modify it    *
 * under the terms version 2 of the GNU General Public License as published   *
 * by the Free Software Foundation. This program is distributed in the hope   *
 * that it will be useful, but WITHOUT ANY WARRANTY, without even the implied *
 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.           *
 * See the GNU General Public License for more details.                       *
 * You should have received a copy of the GNU General Public License along    *
 * with this program, if not, write to the Free Software Foundation, Inc.,    *
 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.                     *
 * For the text or an alternative of this public license, you may reach us    *
 * ComPiere, Inc., 2620 Augustine Dr. #245, Santa Clara, CA 95054, USA        *
 * or via info@compiere.org or http://www.compiere.org/license.html           *
 *****************************************************************************/
/** Generated Model - DO NOT CHANGE */
package org.eevolution.model;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.Properties;
import java.util.logging.Level;

import org.compiere.model.*;
import org.compiere.util.CLogger;
import org.compiere.util.DB;
import org.compiere.util.Env;
import org.compiere.util.KeyNamePair;

/** Generated Model for HR_Employee
 *  @author Adempiere (generated) 
 *  @version Release 3.7.0LTS - $Id$ */
public class X_HR_Employee extends PO implements I_HR_Employee, I_Persistent 
{
	
	/**	Static Logger	*/
	private static CLogger	s_log	= CLogger.getCLogger (X_HR_Employee.class);

	/**
	 *
	 */
	private static final long serialVersionUID = 20120705L;

    /** Standard Constructor */
    public X_HR_Employee (Properties ctx, int HR_Employee_ID, String trxName)
    {
      super (ctx, HR_Employee_ID, trxName);
      /** if (HR_Employee_ID == 0)
        {
			setC_BPartner_ID (0);
			setHR_Employee_ID (0);
			sethrm_wageRate_ID (0);
// 1
			setProcessed (false);
// N
			setStartDate (new Timestamp( System.currentTimeMillis() ));
        } */
    }

    /** Load Constructor */
    public X_HR_Employee (Properties ctx, ResultSet rs, String trxName)
    {
      super (ctx, rs, trxName);
    }

    /** AccessLevel
      * @return 3 - Client - Org 
      */
    protected int get_AccessLevel()
    {
      return accessLevel.intValue();
    }

    /** Load Meta Data */
    protected POInfo initPO (Properties ctx)
    {
      POInfo poi = POInfo.getPOInfo (ctx, Table_ID, get_TrxName());
      return poi;
    }

    public String toString()
    {
      StringBuffer sb = new StringBuffer ("X_HR_Employee[")
        .append(get_ID()).append("]");
      return sb.toString();
    }

	public org.compiere.model.I_C_Activity getC_Activity() throws RuntimeException
    {
		return (org.compiere.model.I_C_Activity)MTable.get(getCtx(), org.compiere.model.I_C_Activity.Table_Name)
			.getPO(getC_Activity_ID(), get_TrxName());	}

	/** Set Activity.
		@param C_Activity_ID 
		Business Activity
	  */
	public void setC_Activity_ID (int C_Activity_ID)
	{
		if (C_Activity_ID < 1) 
			set_Value (COLUMNNAME_C_Activity_ID, null);
		else 
			set_Value (COLUMNNAME_C_Activity_ID, Integer.valueOf(C_Activity_ID));
	}

	/** Get Activity.
		@return Business Activity
	  */
	public int getC_Activity_ID () 
	{
		Integer ii = (Integer)get_Value(COLUMNNAME_C_Activity_ID);
		if (ii == null)
			 return 0;
		return ii.intValue();
	}

	public org.compiere.model.I_C_BPartner getC_BPartner() throws RuntimeException
    {
		return (org.compiere.model.I_C_BPartner)MTable.get(getCtx(), org.compiere.model.I_C_BPartner.Table_Name)
			.getPO(getC_BPartner_ID(), get_TrxName());	}

	/** Set Business Partner .
		@param C_BPartner_ID 
		Identifies a Business Partner
	  */
	public void setC_BPartner_ID (int C_BPartner_ID)
	{
		if (C_BPartner_ID < 1) 
			set_Value (COLUMNNAME_C_BPartner_ID, null);
		else 
			set_Value (COLUMNNAME_C_BPartner_ID, Integer.valueOf(C_BPartner_ID));
	}

	/** Get Business Partner .
		@return Identifies a Business Partner
	  */
	public int getC_BPartner_ID () 
	{
		Integer ii = (Integer)get_Value(COLUMNNAME_C_BPartner_ID);
		if (ii == null)
			 return 0;
		return ii.intValue();
	}

	/** Set Validation code.
		@param Code 
		Validation Code
	  */
	public void setCode (String Code)
	{
		set_Value (COLUMNNAME_Code, Code);
	}

	/** Get Validation code.
		@return Validation Code
	  */
	public String getCode () 
	{
		return (String)get_Value(COLUMNNAME_Code);
	}

	/** Set End Date.
		@param EndDate 
		Last effective date (inclusive)
	  */
	public void setEndDate (Timestamp EndDate)
	{
		set_Value (COLUMNNAME_EndDate, EndDate);
	}

	/** Get End Date.
		@return Last effective date (inclusive)
	  */
	public Timestamp getEndDate () 
	{
		return (Timestamp)get_Value(COLUMNNAME_EndDate);
	}

	public org.eevolution.model.I_HR_Contract getHR_Contract() throws RuntimeException
    {
		return (org.eevolution.model.I_HR_Contract)MTable.get(getCtx(), org.eevolution.model.I_HR_Contract.Table_Name)
			.getPO(getHR_Contract_ID(), get_TrxName());	}

	/** Set Payroll Contract.
		@param HR_Contract_ID Payroll Contract	  */
	public void setHR_Contract_ID (int HR_Contract_ID)
	{
		if (HR_Contract_ID < 1) 
			set_Value (COLUMNNAME_HR_Contract_ID, null);
		else 
			set_Value (COLUMNNAME_HR_Contract_ID, Integer.valueOf(HR_Contract_ID));
	}

	/** Get Payroll Contract.
		@return Payroll Contract	  */
	public int getHR_Contract_ID () 
	{
		Integer ii = (Integer)get_Value(COLUMNNAME_HR_Contract_ID);
		if (ii == null)
			 return 0;
		return ii.intValue();
	}

	public org.eevolution.model.I_HR_Department getHR_Department() throws RuntimeException
    {
		return (org.eevolution.model.I_HR_Department)MTable.get(getCtx(), org.eevolution.model.I_HR_Department.Table_Name)
			.getPO(getHR_Department_ID(), get_TrxName());	}

	/** Set Payroll Department.
		@param HR_Department_ID Payroll Department	  */
	public void setHR_Department_ID (int HR_Department_ID)
	{
		if (HR_Department_ID < 1) 
			set_Value (COLUMNNAME_HR_Department_ID, null);
		else 
			set_Value (COLUMNNAME_HR_Department_ID, Integer.valueOf(HR_Department_ID));
	}

	/** Get Payroll Department.
		@return Payroll Department	  */
	public int getHR_Department_ID () 
	{
		Integer ii = (Integer)get_Value(COLUMNNAME_HR_Department_ID);
		if (ii == null)
			 return 0;
		return ii.intValue();
	}

	/** Set Payroll Employee.
		@param HR_Employee_ID Payroll Employee	  */
	public void setHR_Employee_ID (int HR_Employee_ID)
	{
		if (HR_Employee_ID < 1) 
			set_ValueNoCheck (COLUMNNAME_HR_Employee_ID, null);
		else 
			set_ValueNoCheck (COLUMNNAME_HR_Employee_ID, Integer.valueOf(HR_Employee_ID));
	}

	/** Get Payroll Employee.
		@return Payroll Employee	  */
	public int getHR_Employee_ID () 
	{
		Integer ii = (Integer)get_Value(COLUMNNAME_HR_Employee_ID);
		if (ii == null)
			 return 0;
		return ii.intValue();
	}

	public org.eevolution.model.I_HR_Job getHR_Job() throws RuntimeException
    {
		return (org.eevolution.model.I_HR_Job)MTable.get(getCtx(), org.eevolution.model.I_HR_Job.Table_Name)
			.getPO(getHR_Job_ID(), get_TrxName());	}

	/** Set Payroll Job.
		@param HR_Job_ID Payroll Job	  */
	public void setHR_Job_ID (int HR_Job_ID)
	{
		if (HR_Job_ID < 1) 
			set_Value (COLUMNNAME_HR_Job_ID, null);
		else 
			set_Value (COLUMNNAME_HR_Job_ID, Integer.valueOf(HR_Job_ID));
	}

	/** Get Payroll Job.
		@return Payroll Job	  */
	public int getHR_Job_ID () 
	{
		Integer ii = (Integer)get_Value(COLUMNNAME_HR_Job_ID);
		if (ii == null)
			 return 0;
		return ii.intValue();
	}

	/** Set hrm_confidentialityAgreement.
		@param hrm_confidentialityAgreement hrm_confidentialityAgreement	  */
	public void sethrm_confidentialityAgreement (boolean hrm_confidentialityAgreement)
	{
		set_Value (COLUMNNAME_hrm_confidentialityAgreement, Boolean.valueOf(hrm_confidentialityAgreement));
	}

	/** Get hrm_confidentialityAgreement.
		@return hrm_confidentialityAgreement	  */
	public boolean ishrm_confidentialityAgreement () 
	{
		Object oo = get_Value(COLUMNNAME_hrm_confidentialityAgreement);
		if (oo != null) 
		{
			 if (oo instanceof Boolean) 
				 return ((Boolean)oo).booleanValue(); 
			return "Y".equals(oo);
		}
		return false;
	}

	/** Set hrm_dateOfBirth.
		@param hrm_dateOfBirth hrm_dateOfBirth	  */
	public void sethrm_dateOfBirth (Timestamp hrm_dateOfBirth)
	{
		set_Value (COLUMNNAME_hrm_dateOfBirth, hrm_dateOfBirth);
	}

	/** Get hrm_dateOfBirth.
		@return hrm_dateOfBirth	  */
	public Timestamp gethrm_dateOfBirth () 
	{
		return (Timestamp)get_Value(COLUMNNAME_hrm_dateOfBirth);
	}

	/** hrm_jobCategory AD_Reference_ID=1000001 */
	public static final int HRM_JOBCATEGORY_AD_Reference_ID=1000001;
	/** 1 = 1 */
	public static final String HRM_JOBCATEGORY_1 = "1";
	/** 2 = 2 */
	public static final String HRM_JOBCATEGORY_2 = "2";
	/** 3 = 3 */
	public static final String HRM_JOBCATEGORY_3 = "3";
	/** 4 = 4 */
	public static final String HRM_JOBCATEGORY_4 = "4";
	/** 5 = 5 */
	public static final String HRM_JOBCATEGORY_5 = "5";
	/** 6 = 6 */
	public static final String HRM_JOBCATEGORY_6 = "6";
	/** Set hrm_jobCategory.
		@param hrm_jobCategory hrm_jobCategory	  */
	public void sethrm_jobCategory (String hrm_jobCategory)
	{

		set_Value (COLUMNNAME_hrm_jobCategory, hrm_jobCategory);
	}

	/** Get hrm_jobCategory.
		@return hrm_jobCategory	  */
	public String gethrm_jobCategory () 
	{
		return (String)get_Value(COLUMNNAME_hrm_jobCategory);
	}

	/** Set hrm_liabilityForBreakageSigne.
		@param hrm_liabilityForBreakageSigne hrm_liabilityForBreakageSigne	  */
	public void sethrm_liabilityForBreakageSigne (boolean hrm_liabilityForBreakageSigne)
	{
		set_Value (COLUMNNAME_hrm_liabilityForBreakageSigne, Boolean.valueOf(hrm_liabilityForBreakageSigne));
	}

	/** Get hrm_liabilityForBreakageSigne.
		@return hrm_liabilityForBreakageSigne	  */
	public boolean ishrm_liabilityForBreakageSigne () 
	{
		Object oo = get_Value(COLUMNNAME_hrm_liabilityForBreakageSigne);
		if (oo != null) 
		{
			 if (oo instanceof Boolean) 
				 return ((Boolean)oo).booleanValue(); 
			return "Y".equals(oo);
		}
		return false;
	}

	/** Set hrm_placeOfBirth.
		@param hrm_placeOfBirth hrm_placeOfBirth	  */
	public void sethrm_placeOfBirth (String hrm_placeOfBirth)
	{
		set_Value (COLUMNNAME_hrm_placeOfBirth, hrm_placeOfBirth);
	}

	/** Get hrm_placeOfBirth.
		@return hrm_placeOfBirth	  */
	public String gethrm_placeOfBirth () 
	{
		return (String)get_Value(COLUMNNAME_hrm_placeOfBirth);
	}

	/** hrm_sex AD_Reference_ID=1000000 */
	public static final int HRM_SEX_AD_Reference_ID=1000000;
	/** Женский = Женский */
	public static final String HRM_SEX_Women = "W";
	/** Мужской = Мужской */
	public static final String HRM_SEX_Men = "M";
	/** Set hrm_sex.
		@param hrm_sex hrm_sex	  */
	public void sethrm_sex (String hrm_sex)
	{

		set_Value (COLUMNNAME_hrm_sex, hrm_sex);
	}

	/** Get hrm_sex.
		@return hrm_sex	  */
	public String gethrm_sex () 
	{
		return (String)get_Value(COLUMNNAME_hrm_sex);
	}

	/** Set hrm_staff.
		@param hrm_staff hrm_staff	  */
	public void sethrm_staff (boolean hrm_staff)
	{
		set_Value (COLUMNNAME_hrm_staff, Boolean.valueOf(hrm_staff));
	}

	/** Get hrm_staff.
		@return hrm_staff	  */
	public boolean ishrm_staff () 
	{
		Object oo = get_Value(COLUMNNAME_hrm_staff);
		if (oo != null) 
		{
			 if (oo instanceof Boolean) 
				 return ((Boolean)oo).booleanValue(); 
			return "Y".equals(oo);
		}
		return false;
	}

	public I_hrm_wageRate gethrm_wageRate() throws RuntimeException
    {
		return (I_hrm_wageRate)MTable.get(getCtx(), I_hrm_wageRate.Table_Name)
			.getPO(gethrm_wageRate_ID(), get_TrxName());	}

	/** Set wage-rate.
		@param hrm_wageRate_ID wage-rate	  */
	public void sethrm_wageRate_ID (int hrm_wageRate_ID)
	{
		if (hrm_wageRate_ID < 1) 
			set_Value (COLUMNNAME_hrm_wageRate_ID, null);
		else 
			set_Value (COLUMNNAME_hrm_wageRate_ID, Integer.valueOf(hrm_wageRate_ID));
	}

	/** Get wage-rate.
		@return wage-rate	  */
	public int gethrm_wageRate_ID () 
	{
		Integer ii = (Integer)get_Value(COLUMNNAME_hrm_wageRate_ID);
		if (ii == null)
			 return 0;
		return ii.intValue();
	}

	public org.eevolution.model.I_HR_Payroll getHR_Payroll() throws RuntimeException
    {
		return (org.eevolution.model.I_HR_Payroll)MTable.get(getCtx(), org.eevolution.model.I_HR_Payroll.Table_Name)
			.getPO(getHR_Payroll_ID(), get_TrxName());	}

	/** Set Payroll.
		@param HR_Payroll_ID Payroll	  */
	public void setHR_Payroll_ID (int HR_Payroll_ID)
	{
		if (HR_Payroll_ID < 1) 
			set_Value (COLUMNNAME_HR_Payroll_ID, null);
		else 
			set_Value (COLUMNNAME_HR_Payroll_ID, Integer.valueOf(HR_Payroll_ID));
	}

	/** Get Payroll.
		@return Payroll	  */
	public int getHR_Payroll_ID () 
	{
		Integer ii = (Integer)get_Value(COLUMNNAME_HR_Payroll_ID);
		if (ii == null)
			 return 0;
		return ii.intValue();
	}

	/** Set Image URL.
		@param ImageURL 
		URL of  image
	  */
	public void setImageURL (String ImageURL)
	{
		set_Value (COLUMNNAME_ImageURL, ImageURL);
	}

	/** Get Image URL.
		@return URL of  image
	  */
	public String getImageURL () 
	{
		return (String)get_Value(COLUMNNAME_ImageURL);
	}

	/** Set Name.
		@param Name 
		Alphanumeric identifier of the entity
	  */
	public void setName (String Name)
	{
		set_Value (COLUMNNAME_Name, Name);
	}

	/** Get Name.
		@return Alphanumeric identifier of the entity
	  */
	public String getName () 
	{
		return (String)get_Value(COLUMNNAME_Name);
	}

    /** Get Record ID/ColumnName
        @return ID/ColumnName pair
      */
    public KeyNamePair getKeyNamePair() 
    {
        return new KeyNamePair(get_ID(), getName());
    }

	/** Set Name 2.
		@param Name2 
		Additional Name
	  */
	public void setName2 (String Name2)
	{
		set_Value (COLUMNNAME_Name2, Name2);
	}

	/** Get Name 2.
		@return Additional Name
	  */
	public String getName2 () 
	{
		return (String)get_Value(COLUMNNAME_Name2);
	}

	/** Set National Code.
		@param NationalCode National Code	  */
	public void setNationalCode (String NationalCode)
	{
		set_Value (COLUMNNAME_NationalCode, NationalCode);
	}

	/** Get National Code.
		@return National Code	  */
	public String getNationalCode () 
	{
		return (String)get_Value(COLUMNNAME_NationalCode);
	}

	/** Set Processed.
		@param Processed 
		The document has been processed
	  */
	public void setProcessed (boolean Processed)
	{
		set_Value (COLUMNNAME_Processed, Boolean.valueOf(Processed));
	}

	/** Get Processed.
		@return The document has been processed
	  */
	public boolean isProcessed () 
	{
		Object oo = get_Value(COLUMNNAME_Processed);
		if (oo != null) 
		{
			 if (oo instanceof Boolean) 
				 return ((Boolean)oo).booleanValue(); 
			return "Y".equals(oo);
		}
		return false;
	}

	/** Set Social Security Code.
		@param SSCode Social Security Code	  */
	public void setSSCode (String SSCode)
	{
		set_Value (COLUMNNAME_SSCode, SSCode);
	}

	/** Get Social Security Code.
		@return Social Security Code	  */
	public String getSSCode () 
	{
		return (String)get_Value(COLUMNNAME_SSCode);
	}

	/** Set Start Date.
		@param StartDate 
		First effective day (inclusive)
	  */
	public void setStartDate (Timestamp StartDate)
	{
		set_Value (COLUMNNAME_StartDate, StartDate);
	}

	/** Get Start Date.
		@return First effective day (inclusive)
	  */
	public Timestamp getStartDate () 
	{
		return (Timestamp)get_Value(COLUMNNAME_StartDate);
	}
	
	 /** Load Model for BPartner */
    public static X_HR_Employee getHR_Employee (int C_BPartner_ID, Boolean isActive)
    {
    	if(C_BPartner_ID == 0)
    		return null;
    	String active = "AND HR_EMPLOYEE.ISACTIVE='Y'";
    	if((Boolean) isActive == null)
    		active = "";
    	else if(!isActive)
    		active = "AND HR_EMPLOYEE.ISACTIVE='N'";
    	
    	X_HR_Employee hremp = new X_HR_Employee(Env.getCtx(), 0, null);
    	PreparedStatement pstmt = null;
		ResultSet rs = null;
		
		// Employee data from the card
				String sql_ = "SELECT * FROM HR_EMPLOYEE \n " +
								"WHERE HR_EMPLOYEE.C_BPARTNER_ID=" + C_BPartner_ID + " \n " +
						        "AND HR_EMPLOYEE.ENDDATE = (SELECT MAX(H.ENDDATE) FROM HR_EMPLOYEE H WHERE H.C_BPARTNER_ID=HR_EMPLOYEE.C_BPARTNER_ID) \n " + active;
		try
		{
			pstmt = DB.prepareStatement(sql_,null);
			rs = pstmt.executeQuery();
			if(rs.next())
				hremp = new X_HR_Employee(Env.getCtx(), rs, null);			
		}
		catch (SQLException e)
		{
			s_log.log(Level.SEVERE, "product", e);
		}
		finally
		{
			DB.close(rs, pstmt);
			rs = null; pstmt = null;
		}
		return hremp;	
    }
    
	@Override
	public boolean saveUpdate() {
		return super.saveUpdate();
	}
	
	 public static Timestamp getMaxEndDate (int C_BPartner_ID, Boolean isActive)
	    {
	    	if(C_BPartner_ID == 0)
	    		return null;
	    	String active = "AND HR_EMPLOYEE.ISACTIVE='Y'";
	    	if((Boolean) isActive == null)
	    		active = "";
	    	else if(!isActive)
	    		active = "AND HR_EMPLOYEE.ISACTIVE='N'";
	    	
	    	Timestamp result = null;	    	
	    	PreparedStatement pstmt = null;
			ResultSet rs = null;
			
			// Employee data from the card
					String sql_ = "SELECT ENDDATE FROM HR_EMPLOYEE \n " +
									"WHERE HR_EMPLOYEE.C_BPARTNER_ID=" + C_BPartner_ID + " \n " +
							        "AND HR_EMPLOYEE.ENDDATE = (SELECT MAX(H.ENDDATE) FROM HR_EMPLOYEE H WHERE H.C_BPARTNER_ID=HR_EMPLOYEE.C_BPARTNER_ID) \n " + active;
			try
			{
				pstmt = DB.prepareStatement(sql_,null);
				rs = pstmt.executeQuery();
				if(rs.next())
					result = rs.getTimestamp(1);			
			}
			catch (SQLException e)
			{
				s_log.log(Level.SEVERE, "product", e);
			}
			finally
			{
				DB.close(rs, pstmt);
				rs = null; pstmt = null;
			}
			return result;	
	    }
	 
	 public static Timestamp getMinStartDate (int C_BPartner_ID, Boolean isActive)
	    {
	    	if(C_BPartner_ID == 0)
	    		return null;
	    	
	    	String active = "AND HR_EMPLOYEE.ISACTIVE='Y'";
	    	if((Boolean) isActive == null)
	    		active = "";
	    	else if(!isActive)
	    		active = "AND HR_EMPLOYEE.ISACTIVE='N'";
	    	
	    	Timestamp result = null;	    	
	    	PreparedStatement pstmt = null;
			ResultSet rs = null;
			
			// Employee data from the card
					String sql_ = "SELECT STARTDATE FROM HR_EMPLOYEE \n " +
									"WHERE HR_EMPLOYEE.C_BPARTNER_ID=" + C_BPartner_ID + " \n " +
							        "AND HR_EMPLOYEE.STARTDATE = (SELECT MIN(H.STARTDATE) FROM HR_EMPLOYEE H WHERE H.C_BPARTNER_ID=HR_EMPLOYEE.C_BPARTNER_ID) \n " + active;
			try
			{
				pstmt = DB.prepareStatement(sql_,null);
				rs = pstmt.executeQuery();
				if(rs.next())
					result = rs.getTimestamp(1);			
			}
			catch (SQLException e)
			{
				s_log.log(Level.SEVERE, "product", e);
			}
			finally
			{
				DB.close(rs, pstmt);
				rs = null; pstmt = null;
			}
			return result;	
	    }
}